package com.gadgets.busi.bm01.controller;

import com.gadgets.busi.bm01.common.GadgetsBm01Constant;
import com.gadgets.busi.bm01.common.TxtCopyRestValidateTrigger;
import com.gadgets.busi.bm01.dto.TxtCopyFileDTO;
import com.gadgets.busi.bm01.dto.TxtCopyRestDTO;
import com.gadgets.busi.bm01.service.TxtCopyServiceImpl;
import com.gadgets.busi.bm01.task.Copy1024TxtRunnable;
import com.gadgets.framework.common.exception.GadgetsServiceException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;

/**
 * The type Txt 1024 copy controller.
 *
 * @author UncleXiu
 * @version V1.0.0
 * @data 2020 -09-21
 */
//@RoleAuthCheck
@RestController
@RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/1024")
public class Txt1024CopyControllerImpl implements ITxtCopyController {

    private static final Logger logger = LoggerFactory.getLogger(Txt1024CopyControllerImpl.class);

    @Autowired
    private TxtCopyRestValidateTrigger txtCopyRestValidateTrigger;
    @Autowired
    private TxtCopyServiceImpl txtCopyService;
    @Value("${txt.1024.targetPath}")
    private String path;

    /**
     * 文件上传基础路径
     */
    @Value("${spring.uploadPath}")
    private String uploadPath;
    @Value("${spring.downloadPath}")
    private String downloadPath;

    /**
     * 文本拷贝请求（RestFul请求）
     *
     * @param request        网络请求信息
     * @param response       网络应答信息
     * @param txtCopyRestDTO 文本RestFul请求信息
     *
     * @throws GadgetsServiceException 异常信息
     */
    //@RoleAuthCheck
    @RequestMapping(method = {org.springframework.web.bind.annotation.RequestMethod.POST,org.springframework.web.bind.annotation.RequestMethod.GET},value = "/txtCopy.do")
    @Override
    public void requestTxtCopy(HttpServletRequest request, HttpServletResponse response, TxtCopyRestDTO txtCopyRestDTO) throws GadgetsServiceException {
        // 执行校验
        txtCopyRestValidateTrigger.validate(txtCopyRestDTO);
        String[] targetPaths = txtCopyRestDTO.getPath().split(",");
        if(GadgetsBm01Constant.COPY_MODUEL.MULTI_COPY.equals(txtCopyRestDTO.getCopyModuel())){
            // 组合拷贝
            logger.info("执行组合拷贝");
            Copy1024TxtRunnable runnable =  new Copy1024TxtRunnable();
            runnable.setModuelNo(GadgetsBm01Constant.BM01_MODUEL_NO);
            runnable.setTargetSite(txtCopyRestDTO.getTargetSite());
            runnable.setPath(targetPaths);
            runnable.setTitle(txtCopyRestDTO.getArticalName());
            runnable.setWritePath(path);
            runnable.setCopyModuel(GadgetsBm01Constant.COPY_MODUEL.MULTI_COPY);
            txtCopyService.submitTxtCopyTask(runnable);
        }else {
            logger.info("执行独立拷贝");
            for (String targetPath : targetPaths) {
                // 独立拷贝
                Copy1024TxtRunnable runnable =  new Copy1024TxtRunnable();
                runnable.setModuelNo(GadgetsBm01Constant.BM01_MODUEL_NO);
                runnable.setTargetSite(txtCopyRestDTO.getTargetSite());
                runnable.setTitle(txtCopyRestDTO.getArticalName());
                runnable.setPath(new String[]{targetPath});
                runnable.setWritePath(path);
                runnable.setCopyModuel(GadgetsBm01Constant.COPY_MODUEL.SINGLE_COPY);
                txtCopyService.submitTxtCopyTask(runnable);
            }
        }
    }

    /**
     * 文本拷贝请求（文本文件请求）
     *
     * @param request        网络请求信息
     * @param response       网络应答信息
     * @param txtCopyFileDTO 文件请求信息
     *
     * @throws GadgetsServiceException 异常信息
     */
    //@RoleAuthCheck
    @Override
    public void requestTxtCopy(HttpServletRequest request, HttpServletResponse response, TxtCopyFileDTO txtCopyFileDTO) throws GadgetsServiceException {
        File file = new File(uploadPath+"/"+txtCopyFileDTO.getFileName()+"."+txtCopyFileDTO.getFileSuffix());
        if(file.exists()){
            // 解析读取文件

        }
    }

}
